﻿using System;
using System.Collections.Generic;
using System.Linq;
using Assist;
using Autodesk.Revit.DB;
using YJKRoomInspection.Model;
using YJKRoomInspection.Utiltiy;
using YJKRevitKernel.ElementSearchUtility.LinkDocumentSuite;
using YJKGeometryFuncs;
using YJKGeometryFuncs.CurveUtility;
using YJKInspectModelFunc;

namespace YJKRoomInspection.FireAreOutLineAppend
{
	internal static class BaseChecker
	{
		internal static List<Def_Sel> GetOnOutLineWinDoorWall(ViewPlan useViewPlan)
		{
			List<Def_Sel> result = new List<Def_Sel>();
			List<Area> allAreaByView = YJKInspectModelFunc.Utility.GetAllAreaByView(useViewPlan);
			if (allAreaByView.Count == 0)
			{
				return result;
			}
			List<Curve> crossSideSegFromAreas = CurveUtility.GetCrossSideSegFromAreas(allAreaByView);
			if (crossSideSegFromAreas.Count == 0)
			{
				return result;
			}
			List<BaseLinkDocumentResultBean> allWallByView = YJKInspectModelFunc.Utility.GetAllWallByView(useViewPlan);
			List<BaseLinkDocumentResultBean> list = new List<BaseLinkDocumentResultBean>();
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in allWallByView)
			{
				using (List<Curve>.Enumerator enumerator2 = crossSideSegFromAreas.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						if (CurveUtility.IsCurveCrossWall(enumerator2.Current, baseLinkDocumentResultBean.UseElement, baseLinkDocumentResultBean.UseLinkeInstance, baseLinkDocumentResultBean.GetTotalTransform()))
						{
							bool flag = false;
							using (List<BaseLinkDocumentResultBean>.Enumerator enumerator3 = list.GetEnumerator())
							{
								while (enumerator3.MoveNext())
								{
									if ((enumerator3.Current.UseElement as Wall).Id == (baseLinkDocumentResultBean.UseElement as Wall).Id)
									{
										flag = true;
										break;
									}
								}
							}
							if (!flag)
							{
								list.Add(baseLinkDocumentResultBean);
							}
						}
					}
				}
			}
			List<BaseLinkDocumentResultBean> allWinDoorByView = YJKInspectModelFunc.Utility.GetAllWinDoorByView(useViewPlan);
			return BaseChecker.GetOnOutLineWinDoorWallPakcers(useViewPlan, crossSideSegFromAreas, list, allWinDoorByView);
		}

		private static List<Def_Sel> GetOnOutLineWinDoorWallPakcers(ViewPlan useViewPlan, List<Curve> lstCrossCurves, List<BaseLinkDocumentResultBean> lstWallCross, List<BaseLinkDocumentResultBean> lstWinDoors)
		{
			List<Def_Sel> list = new List<Def_Sel>();
			foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean in lstWallCross)
			{
				Wall ele = baseLinkDocumentResultBean.UseElement as Wall;
				Def_Sel define = BaseChecker.GetDefine(useViewPlan, baseLinkDocumentResultBean);
				if (!YJKRoomInspection.Utiltiy.Utility.IsValid3HourFireWall(ele))
				{
					list.Add(define);
				}
				foreach (BaseLinkDocumentResultBean baseLinkDocumentResultBean2 in from elem in lstWinDoors
				where (elem.UseElement as FamilyInstance).Host.Id == ele.Id
				select elem)
				{
					FamilyInstance familyInstance = baseLinkDocumentResultBean2.UseElement as FamilyInstance;
					string name = familyInstance.Symbol.Family.Name;
					string name2 = familyInstance.Symbol.Name;
					if (!YJKRoomInspection.Utiltiy.Utility.IsValidDoorWinClassAFamilyName(name, name2))
					{
						XYZ xyz;
						if (baseLinkDocumentResultBean2.UseLinkeInstance == null)
						{
							xyz = (familyInstance.Location as LocationPoint).Point;
						}
						else
						{
							LocationPoint locationPoint = familyInstance.Location as LocationPoint;
							xyz = baseLinkDocumentResultBean2.GetTotalTransform().OfPoint(locationPoint.Point);
						}
						xyz = new XYZ(xyz.X, xyz.Y, 0.0);
						bool flag = false;
						foreach (Curve curve in lstCrossCurves)
						{
							XYZ xyz2 = curve.Project(xyz).XYZPoint;
							xyz2 = new XYZ(xyz2.X, xyz2.Y, 0.0);
							if (Geometry.IsEqual(xyz, xyz2, 0.0001))
							{
								flag = true;
								break;
							}
						}
						if (flag)
						{
							Def_Sel define2 = BaseChecker.GetDefine(useViewPlan, baseLinkDocumentResultBean2);
							bool flag2 = false;
							foreach (Def_Sel def_Sel in list)
							{
								if (define2.m_element.Id == def_Sel.m_element.Id)
								{
									flag2 = true;
									break;
								}
							}
							if (!flag2)
							{
								list.Add(define2);
							}
						}
					}
				}
			}
			return list;
		}

		private static Def_Sel GetDefine(ViewPlan view, BaseLinkDocumentResultBean tmpElementBean)
		{
			Def_Sel def_Sel = new Def_Sel();
			def_Sel.m_viewId = view.Id;
			def_Sel.m_strViewName = RevitVersionFuncs.GetViewName(view);
			Element useElement = tmpElementBean.UseElement;
			def_Sel.m_element = useElement;
			if (tmpElementBean.UseLinkeInstance == null)
			{
				def_Sel.m_RevitLinkInstance = tmpElementBean.UseLinkeInstance;
				def_Sel.m_bIsLinkEle = false;
			}
			else
			{
				def_Sel.m_RevitLinkInstance = tmpElementBean.UseLinkeInstance;
				def_Sel.m_bIsLinkEle = true;
			}
			FamilyInstance familyInstance = useElement as FamilyInstance;
			string name;
			string text;
			if (familyInstance != null)
			{
				name = familyInstance.Symbol.Name;
				text = familyInstance.Symbol.Family.Name;
			}
			else
			{
				name = useElement.Name;
				text = name;
			}
			int integerValue = useElement.Category.Id.IntegerValue;
			string strEleName;
			if (integerValue == -2000014)
			{
				def_Sel.m_kType = EleType.eWindow;
				strEleName = text + " " + name;
			}
			else if (integerValue == -2000011)
			{
				def_Sel.m_kType = EleType.eWall;
				strEleName = text;
			}
			else
			{
				strEleName = text + " " + name;
				if (text.IndexOf("卷帘") >= 0)
				{
					def_Sel.m_kType = EleType.eDoorRolling;
				}
				else
				{
					def_Sel.m_kType = EleType.eDoor;
				}
			}
			def_Sel.m_strEleName = strEleName;
			return def_Sel;
		}
	}
}
